package ext.acae2.util;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;

import wt.log4j.LogR;

public class LoadingXmlUtil {

	private static final Logger LOGGER = LogR.getLogger(LoadingXmlUtil.class
			.getName());

	public static Document execute(String path) throws Exception {

		LOGGER.debug("<<< Start loadingXml");

		// remove nameSpace in Xml
		String newPath = RemoveNSUtil.remove(path);
		LOGGER.debug("path: " + path);

		// load XML to DOM Document
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();

		File file = new File(newPath);
		Document doc = builder.parse(file);

		LOGGER.debug("newPath: " + newPath);
		LOGGER.debug(">>> End loadingXml");

		return doc;

	}
	
	/**
	 * 
	 * @description dom4j and xpath parse XML
	 * @author cnie
	 * @date 2013-4-22 下午12:59:43
	 * @version 1.0
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static org.dom4j.Document executeForDom4j(String path) throws Exception {

		LOGGER.debug("<<< Start loadingXml");

		// remove nameSpace in Xml
		String newPath = RemoveNSUtil.remove(path);
		LOGGER.debug("path: " + path);
		
		File file = new File(newPath);

		org.dom4j.Document doc = new SAXReader().read(file);

		LOGGER.debug("newPath: " + newPath);
		LOGGER.debug(">>> End loadingXml");

		return doc;

	}
	
	
}
